import { createStore } from "vuex";
import createPersistedState from "vuex-persistedstate";

export default createStore({
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
      //设置哪些内容同步到sessionStorage
      reducer(val) {
        return {
          token: val.token,
          menu: val.menu,
          user_info: val.user_info,
          time: val.time,
          role_list: val.role_list,
        };
      },
    }),
  ],
  state: {
    token: null,
    menu: [],
    tabs: [], //选中标签
    tab_active: "",
    username: "",
    passwd: "",
    user_info: {},
    time: "",
    role_list: [],
  },

  // set方法 其中state相当于this
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token;
    },
    SET_MENU(state, menu) {
      state.menu = menu;
    },
    SET_USERNAME(state, username) {
      state.username = username;
    },
    SET_PASSWD(state, passwd) {
      state.passwd = passwd;
    },
    SET_USERINFO(state, userInfo) {
      state.user_info = userInfo;
    },
    SET_TIME(state, time) {
      state.time = time;
    },
    SET_ROLELIST(state, role_list) {
      state.role_list = role_list;
    },
    ADD_TABS(state, item) {
      for (let i = 0; i < state.tabs.length; i++) {
        state.tab_active = item.name;
        //如果出现重复项，不添加
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        if (state.tabs[i].name == item.name) {
          return;
        }
      }
      //追加数据
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      state.tabs.push(item);
      //此时激活的数据就是刚添加的数据
      state.tab_active = item.name;
    },
    SET_TAB_ACTIVE(state, active) {
      state.tab_active = active;
    },
    REMOVE_TAB(state, name) {
      for (let i = 0; i < state.tabs.length; i++) {
        //如果出现name相同的项，删除数据
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        if (state.tabs[i].name == name) {
          state.tabs.splice(i, 1);
          if (state.tab_active === name) {
            //删除完毕后，判断是否还有标签，有的话才激活
            if (state.tabs.length > 0) {
              //获取后一个，后一个没有获取前一个
              const newitem = state.tabs[i] || state.tabs[i - 1];
              // eslint-disable-next-line @typescript-eslint/ban-ts-comment
              // @ts-ignore
              state.tab_active = newitem.name;
            }
            return;
          }
        }
      }
    },
    LOGOUT(state) {
      state.token = null;
      state.menu = [];
      state.tabs = [];
      state.tab_active = "";
      state.username = "";
      state.passwd = "";
      state.user_info = {};
      state.time = "";
      state.role_list = [];
    },
  },
  actions: {
    setToken(state, token) {
      state.commit("SET_TOKEN", token);
    },
    setMenu(state, menu) {
      state.commit("SET_MENU", menu);
    },
    setUsername(state, username) {
      state.commit("SET_USERNAME", username);
    },
    setPasswd(state, passwd) {
      state.commit("SET_PASSWD", passwd);
    },
    setUserInfo(state, userInfo) {
      state.commit("SET_USERINFO", userInfo);
    },
    setTime(state, time) {
      state.commit("SET_TIME", time);
    },
    setRoleList(state, role_list) {
      state.commit("SET_ROLELIST", role_list);
    },
    addTabs(state, item) {
      state.commit("ADD_TABS", item);
    },
    setTabActive(state, active) {
      state.commit("SET_TAB_ACTIVE", active);
    },
    removeTab(state, name) {
      state.commit("REMOVE_TAB", name);
    },
    logout(state) {
      state.commit("LOGOUT");
    },
  },
  getters: {},
  modules: {},
});
